/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package vistas;

import controladores.VehiculoJpaController;
import entidades.Categoria;
import entidades.Chofer;
import entidades.Marca;
import entidades.Modelo;
import entidades.Obra;
import entidades.Propietario;
import entidades.Tanque;
import entidades.TipoCombustible;
import entidades.Vehiculo;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
import reportes.GenReportes;
import rn.CombustibleRN;
import rn.VehiculoRN;

/**
 *
 * @author Pablo
 */
public class FormSalidaCombustible extends javax.swing.JFrame {

    private Vehiculo vehiculo, vehiculo_seleccionado;
    private Tanque tanque_selected;
    DefaultTableModel modelo_de_tabla;
    private int opcion_modificar = 0;

    public FormSalidaCombustible() {
        initComponents();
        

        vehiculo = new Vehiculo();
        
        tf_descripcion.setDocument(new JTextFieldToUpperCase());
        tf_nro_unidad.setDocument(new JTextFieldToUpperCase());
        tf_chapa.setDocument(new JTextFieldToUpperCase());
        tf_chofer.setDocument(new JTextFieldToUpperCase());
       
        tf_descripcion.getDocument().putProperty("campoDelEvento", "descripcion");
        tf_descripcion.getDocument().addDocumentListener(new TexChangeListener());
        tf_descripcion.addKeyListener(new KeyPressedClass());
        tf_nro_unidad.getDocument().putProperty("campoDelEvento", "nro_unidad");
        tf_nro_unidad.getDocument().addDocumentListener(new TexChangeListener());
        tf_nro_unidad.addKeyListener(new KeyPressedClass());
        tf_chapa.getDocument().putProperty("campoDelEvento", "nro_chapa");
        tf_chapa.getDocument().addDocumentListener(new TexChangeListener());
        tf_chapa.addKeyListener(new KeyPressedClass());
        tf_chofer.getDocument().putProperty("campoDelEvento", "chofer");
        tf_chofer.getDocument().addDocumentListener(new TexChangeListener());
        tf_chofer.addKeyListener(new KeyPressedClass());
        
        //tf_litros_a_cargar.addKeyListener(new KeyPressedEnterClass());        

        tabla_conincidencias.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "Enter");
        tabla_conincidencias.getActionMap().put("Enter", new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                System.out.println("Se presiono la tecla enter");
                int fila = tabla_conincidencias.getSelectedRow();
                String s_id = (String) tabla_conincidencias.getValueAt(fila, 0);
                vehiculo_seleccionado = VehiculoRN.findVehiculoPorID(new Long(s_id));
                llenarCampos(vehiculo_seleccionado);
                habilitarCampos(false);
                tabla_conincidencias.requestFocus();
                tabla_conincidencias.changeSelection(fila, 0, false, false);
                tf_litros_a_cargar.requestFocus();
            }
        });
        
        tf_hora.setText(getHora());
        
        llenarCombos();
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        getRootPane().setDefaultButton(bt_ingresar);
        tf_descripcion.requestFocus();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel3 = new javax.swing.JLabel();
        tf_combustible_actual1 = new javax.swing.JTextField();
        bt_ingresar1 = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        tf_combustible_actual4 = new javax.swing.JTextField();
        bt_cancelar = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        tf_descripcion = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        tf_chapa = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        tf_nro_unidad = new javax.swing.JTextField();
        jLabel15 = new javax.swing.JLabel();
        tf_chofer = new javax.swing.JTextField();
        jLabel16 = new javax.swing.JLabel();
        tf_modelo = new javax.swing.JTextField();
        tf_marca = new javax.swing.JTextField();
        jLabel17 = new javax.swing.JLabel();
        bt_limpiar = new javax.swing.JButton();
        jScrollPane4 = new javax.swing.JScrollPane();
        tabla_conincidencias = new javax.swing.JTable();
        jPanel4 = new javax.swing.JPanel();
        combo_listados = new javax.swing.JComboBox();
        tx_obra = new javax.swing.JLabel();
        combo_obra_filtro = new javax.swing.JComboBox();
        combo_categoria_filtro = new javax.swing.JComboBox();
        tx_categoria = new javax.swing.JLabel();
        tx_propietario = new javax.swing.JLabel();
        combo_propietario_filtro = new javax.swing.JComboBox();
        jPanel5 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        combo_tanques = new javax.swing.JComboBox();
        tf_combustible_disponible = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        tf_litros_a_cargar = new javax.swing.JTextField();
        bt_ingresar = new javax.swing.JButton();
        fecha = new org.freixas.jcalendar.JCalendarCombo();
        jLabel7 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        tf_hora = new javax.swing.JTextField();
        tf_tipo_combustible = new javax.swing.JTextField();

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel3.setText("Combustible Actual (M3):");

        tf_combustible_actual1.setEnabled(false);

        bt_ingresar1.setText("INGRESAR");

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel5.setText("Ingreso (M3):");

        jLabel9.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel9.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel9.setText("Ingreso (M3):");

        tf_combustible_actual4.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        bt_cancelar.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        bt_cancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/cancel.png"))); // NOI18N
        bt_cancelar.setText("CANCELAR");
        bt_cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_cancelarActionPerformed(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(0, 0, 153));

        jLabel2.setBackground(new java.awt.Color(255, 255, 255));
        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(255, 255, 255));
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("SALIDA DE COMBUSTIBLE");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        tf_descripcion.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N

        jLabel8.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel8.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel8.setText("Descripción:");

        jLabel12.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel12.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel12.setText("Chapa:");

        tf_chapa.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N

        jLabel13.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel13.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel13.setText("Nº de unidad:");

        tf_nro_unidad.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N

        jLabel15.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel15.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel15.setText("Chofer:");

        tf_chofer.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N

        jLabel16.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel16.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel16.setText("Modelo:");

        tf_modelo.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N

        tf_marca.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N

        jLabel17.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel17.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel17.setText("Marca:");

        bt_limpiar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        bt_limpiar.setText("LIMPIAR");
        bt_limpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_limpiarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(66, 66, 66)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel13, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel12)
                            .addComponent(jLabel8)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addGap(49, 49, 49)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jLabel17, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel15, javax.swing.GroupLayout.Alignment.TRAILING))
                            .addComponent(jLabel16, javax.swing.GroupLayout.Alignment.TRAILING))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(tf_marca, javax.swing.GroupLayout.DEFAULT_SIZE, 269, Short.MAX_VALUE)
                    .addComponent(tf_chofer, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(tf_nro_unidad, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(tf_chapa, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(tf_descripcion, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(tf_modelo))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGap(0, 255, Short.MAX_VALUE)
                .addComponent(bt_limpiar)
                .addGap(110, 110, 110))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tf_descripcion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tf_chapa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel12))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tf_nro_unidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel13))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tf_chofer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel15))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tf_marca, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel17))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tf_modelo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel16))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(bt_limpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        tabla_conincidencias.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane4.setViewportView(tabla_conincidencias);

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Listar"));

        combo_listados.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        combo_listados.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Seleccionar...", "Todos", "Vehiculos en Planta", "Propietario:", "Vehiculos en obra:", "Categoria:" }));
        combo_listados.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                combo_listadosActionPerformed(evt);
            }
        });

        tx_obra.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        tx_obra.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        tx_obra.setText("Obra actual:");
        tx_obra.setEnabled(false);

        combo_obra_filtro.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        combo_obra_filtro.setEnabled(false);
        combo_obra_filtro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                combo_obra_filtroActionPerformed(evt);
            }
        });

        combo_categoria_filtro.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        combo_categoria_filtro.setEnabled(false);
        combo_categoria_filtro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                combo_categoria_filtroActionPerformed(evt);
            }
        });

        tx_categoria.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        tx_categoria.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        tx_categoria.setText("Categ.:");
        tx_categoria.setEnabled(false);

        tx_propietario.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        tx_propietario.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        tx_propietario.setText("Propietariol:");
        tx_propietario.setEnabled(false);

        combo_propietario_filtro.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        combo_propietario_filtro.setEnabled(false);
        combo_propietario_filtro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                combo_propietario_filtroActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(combo_listados, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(tx_obra))
                    .addComponent(tx_propietario))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(combo_propietario_filtro, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(combo_obra_filtro, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(tx_categoria)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(combo_categoria_filtro, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(combo_categoria_filtro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(tx_categoria))
                    .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(combo_listados, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(combo_obra_filtro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(tx_obra)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(combo_propietario_filtro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tx_propietario)))
        );

        jPanel5.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel4.setText("Disponible (Ltrs):");

        jLabel10.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel10.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel10.setText("Combustible:");

        jLabel11.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel11.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel11.setText("Tanque:");

        combo_tanques.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        combo_tanques.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                combo_tanquesActionPerformed(evt);
            }
        });

        tf_combustible_disponible.setEditable(false);
        tf_combustible_disponible.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel6.setText("Litros a cargar:");

        tf_litros_a_cargar.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        tf_litros_a_cargar.setForeground(new java.awt.Color(0, 0, 255));

        bt_ingresar.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        bt_ingresar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/accept.png"))); // NOI18N
        bt_ingresar.setText("CARGAR");
        bt_ingresar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_ingresarActionPerformed(evt);
            }
        });

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel7.setText("Fecha:");

        jLabel14.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel14.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel14.setText("Hora:");

        tf_hora.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N

        tf_tipo_combustible.setEditable(false);
        tf_tipo_combustible.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
                .addGap(116, 116, 116)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(bt_ingresar)
                        .addGap(86, 86, 86))
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addGap(13, 13, 13)
                        .addComponent(jLabel14)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tf_hora)
                        .addContainerGap())
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(fecha, javax.swing.GroupLayout.PREFERRED_SIZE, 265, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10)
                    .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(combo_tanques, javax.swing.GroupLayout.Alignment.TRAILING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(tf_litros_a_cargar)
                    .addComponent(tf_combustible_disponible, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(tf_tipo_combustible, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(combo_tanques, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(tf_tipo_combustible, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(tf_combustible_disponible, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(tf_litros_a_cargar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(fecha, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel14)
                    .addComponent(tf_hora, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(14, 14, 14)
                .addComponent(bt_ingresar, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(5, 5, 5))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane4)
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(600, 600, 600)
                        .addComponent(bt_cancelar)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 362, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(bt_cancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public boolean verificarDatosVehiculo(Vehiculo v, Long id_excepcion){
        boolean b = true;     
        if (VehiculoRN.yaExiste(v.getDescripcion(), VehiculoJpaController.DESCRIPCION, id_excepcion)) {
                b = false;
                mostrarMensaje("Ya existe un vehiculo con la misma 'descripción'");
            }
            if (VehiculoRN.yaExiste(v.getChapa(), VehiculoJpaController.NRO_CHAPA, id_excepcion)) {
                b = false;
                mostrarMensaje("Ya existe un vehiculo con la misma 'chapa'");
            }
            if (VehiculoRN.yaExiste(v.getNro_chasis(), VehiculoJpaController.CHASIS, id_excepcion)) {
                b = false;
                mostrarMensaje("Ya existe un vehiculo con la misma 'chasis'");
            }
            if (VehiculoRN.yaExiste(v.getNro_unidad(), VehiculoJpaController.NRO_UNIDAD, id_excepcion)) {
                b = false;
                mostrarMensaje("Ya existe un vehiculo con la misma 'nro_unidad'");
            }
       return b;
    }
    
    private void combo_listadosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_combo_listadosActionPerformed
        List<Vehiculo> resultado = new ArrayList();
        String item_selected = (String) combo_listados.getSelectedItem();
        if (!item_selected.equals("Seleccionar...")) {
            switch (item_selected) {
                case "Todos":
                    habilitarFiltro(false);
                    resultado = VehiculoRN.getTodosVechiculos();                    
                    break;
                case "Vehículos de Hormicat":
                    habilitarFiltro(false);
                    resultado = VehiculoRN.getVehiculosPor("HORMICAT S.A", "Propietario");                    
                    break;
                case "Vehículos externos":
                    habilitarFiltro(false);
                    resultado = VehiculoRN.getVehiculosPor("Hormicat S.A.", item_selected);                    
                    break;
                case "Vehiculos en Planta":
                    habilitarFiltro(false);
                    resultado = VehiculoRN.getVehiculosPor("Planta", "Obra");                    
                    break;
                case "Propietario:":
                    tx_obra.setEnabled(false);
                    combo_obra_filtro.setEnabled(false); 
                    tx_categoria.setEnabled(false);
                    combo_categoria_filtro.setEnabled(false);
                    tx_propietario.setEnabled(true);
                    combo_propietario_filtro.setEnabled(true);                  
                    break;    
                case "Vehiculos en obra:":
                    tx_obra.setEnabled(true);
                    combo_obra_filtro.setEnabled(true); 
                    tx_categoria.setEnabled(false);
                    combo_categoria_filtro.setEnabled(false);
                    tx_propietario.setEnabled(false);
                    combo_propietario_filtro.setEnabled(false);   
                    break;
                case "Categoria:":
                    tx_categoria.setEnabled(true);
                    combo_categoria_filtro.setEnabled(true);
                    tx_obra.setEnabled(false);
                    combo_obra_filtro.setEnabled(false);
                    tx_propietario.setEnabled(false);
                    combo_propietario_filtro.setEnabled(false);  
                    break;
                case "Autos":
                    habilitarFiltro(false);
                    resultado = VehiculoRN.getVehiculosPor("Auto", "Categoria");                    
                    break;
                case "Camiones":
                    habilitarFiltro(false);
                    resultado = VehiculoRN.getVehiculosPor("Camion", "Categoria");
                    break;
                case "Hormigoneros":
                    habilitarFiltro(false);
                    resultado = VehiculoRN.getVehiculosPor("Hormigonero", "Categoria");
                    break;             
            }
            llenarTablaResultado(resultado);
        }
    }//GEN-LAST:event_combo_listadosActionPerformed

    private void habilitarFiltro(boolean b) {
        tx_obra.setEnabled(b);
        combo_obra_filtro.setEnabled(b);
        tx_categoria.setEnabled(b);
        combo_categoria_filtro.setEnabled(b);
        tx_propietario.setEnabled(b);
        combo_propietario_filtro.setEnabled(b);  
    }
    
    private void combo_obra_filtroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_combo_obra_filtroActionPerformed
        List<Vehiculo> resultado = new ArrayList();
        Obra item_selected = (Obra) combo_obra_filtro.getSelectedItem();
        resultado = VehiculoRN.getVehiculosPor(item_selected.toString(), "Obra");
        llenarTablaResultado(resultado);        
    }//GEN-LAST:event_combo_obra_filtroActionPerformed

    private void combo_categoria_filtroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_combo_categoria_filtroActionPerformed
        List<Vehiculo> resultado = new ArrayList();
        Categoria item_selected = (Categoria) combo_categoria_filtro.getSelectedItem();
        resultado = VehiculoRN.getVehiculosPor(item_selected.toString(), "Categoria");
        llenarTablaResultado(resultado);    
    }//GEN-LAST:event_combo_categoria_filtroActionPerformed

    private void combo_propietario_filtroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_combo_propietario_filtroActionPerformed
        List<Vehiculo> resultado = new ArrayList();
        Propietario item_selected = (Propietario) combo_propietario_filtro.getSelectedItem();
        resultado = VehiculoRN.getVehiculosPor(item_selected.toString(), "Propietario");
        llenarTablaResultado(resultado); 
    }//GEN-LAST:event_combo_propietario_filtroActionPerformed

    private void bt_limpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_limpiarActionPerformed
        limpiarCampos();
        iniciarModeloTabla();
        tabla_conincidencias.setModel(modelo_de_tabla);
        habilitarCampos(true);
        opcion_modificar = 0;
        vehiculo_seleccionado = null;
        
//        
//        List<Tanque> todosVechiculos = CombustibleRN.getTodosVechiculos();
//        llenarCombo(combo_tanques, todosVechiculos);
        
        tf_descripcion.requestFocus();
    }//GEN-LAST:event_bt_limpiarActionPerformed

    private void combo_tanquesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_combo_tanquesActionPerformed
        if (combo_tanques.getSelectedItem() != null) {
            tanque_selected = (Tanque) combo_tanques.getSelectedItem();
            tf_tipo_combustible.setText(tanque_selected.getTipoCombustible().getDescripcion());
            tf_combustible_disponible.setText("" + tanque_selected.getDisponible_litros());
        }
    }//GEN-LAST:event_combo_tanquesActionPerformed

    private void bt_ingresarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_ingresarActionPerformed
        int op = JOptionPane.showConfirmDialog(null, "Confirmar carga?");
        if (op == 0) {
            efectuarCarga();
            
        }

    }//GEN-LAST:event_bt_ingresarActionPerformed

    private void bt_cancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_cancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_bt_cancelarActionPerformed

    private void setearVehiculo() {
        //Fields
        vehiculo.setDescripcion(tf_descripcion.getText());
        vehiculo.setChapa(tf_chapa.getText());
        vehiculo.setNro_unidad(tf_nro_unidad.getText());
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bt_cancelar;
    private javax.swing.JButton bt_ingresar;
    private javax.swing.JButton bt_ingresar1;
    private javax.swing.JButton bt_limpiar;
    private javax.swing.JComboBox combo_categoria_filtro;
    private javax.swing.JComboBox combo_listados;
    private javax.swing.JComboBox combo_obra_filtro;
    private javax.swing.JComboBox combo_propietario_filtro;
    private javax.swing.JComboBox combo_tanques;
    private org.freixas.jcalendar.JCalendarCombo fecha;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTable tabla_conincidencias;
    private javax.swing.JTextField tf_chapa;
    private javax.swing.JTextField tf_chofer;
    private javax.swing.JTextField tf_combustible_actual1;
    private javax.swing.JTextField tf_combustible_actual4;
    private javax.swing.JTextField tf_combustible_disponible;
    private javax.swing.JTextField tf_descripcion;
    private javax.swing.JTextField tf_hora;
    private javax.swing.JTextField tf_litros_a_cargar;
    private javax.swing.JTextField tf_marca;
    private javax.swing.JTextField tf_modelo;
    private javax.swing.JTextField tf_nro_unidad;
    private javax.swing.JTextField tf_tipo_combustible;
    private javax.swing.JLabel tx_categoria;
    private javax.swing.JLabel tx_obra;
    private javax.swing.JLabel tx_propietario;
    // End of variables declaration//GEN-END:variables


    private void limpiarCampos() {
        tf_descripcion.setText(null);
        tf_chapa.setText(null);
        tf_nro_unidad.setText(null);
        tf_chofer.setText(null);
        tf_marca.setText(null);
        tf_modelo.setText(null);
        tf_litros_a_cargar.setText("");
        
    }

    private void mostrarMensaje(String mensaje) {
        JOptionPane.showMessageDialog(null, mensaje);
    }

    private void llenarCombos() {
        List<Categoria> todosCategoria = VehiculoRN.getTodosCategoria();
        List<Obra> todosObra = VehiculoRN.getTodosObra();
        List<Propietario> todosPropietario = VehiculoRN.getTodosPropietario();
        List<Tanque> todosVechiculos = CombustibleRN.getTodosVechiculos();
   

        llenarCombo(combo_obra_filtro, todosObra);
        llenarCombo(combo_categoria_filtro, todosCategoria);
        llenarCombo(combo_propietario_filtro, todosPropietario);
        llenarCombo(combo_tanques, todosVechiculos);

    }

    public void llenarCombo(JComboBox combo, List lista) {
        for (Object c : lista) {
            combo.addItem(c);
        }
    }

    private void llenarTablaResultado(List<Vehiculo> resultado) {
        iniciarModeloTabla();
        for (Vehiculo c : resultado) {

            String fila[] = new String[]{
                c.getId().toString(), c.getDescripcion(), c.getNro_unidad(), c.getChapa() , c.getNro_chasis()
            };
            modelo_de_tabla.addRow(fila);
        }
        tabla_conincidencias.setModel(modelo_de_tabla);
        iniciarModeloColumna();
        
    }

   

    public class TexChangeListener implements DocumentListener {

        public void insertUpdate(javax.swing.event.DocumentEvent evt) {
            LlenarTablaConicidencias(evt);
        }

        public void removeUpdate(javax.swing.event.DocumentEvent evt) {
            LlenarTablaConicidencias(evt);
        }

        public void changedUpdate(javax.swing.event.DocumentEvent evt) {
            LlenarTablaConicidencias(evt);
        }
    }

    private void iniciarModeloTabla() {
        Object datos[][] = {};
        String[] columnNames = {"Id", "Descripcion", "Nº Unidad", "Nº Chapa", "Nº Chasis"};
        modelo_de_tabla = new DefaultTableModel(datos, columnNames);        
    }
    
    private void iniciarModeloColumna(){
        TableColumnModel columnModel = tabla_conincidencias.getColumnModel();
        columnModel.getColumn(0).setPreferredWidth(10);
        columnModel.getColumn(2).setPreferredWidth(20);
        columnModel.getColumn(3).setPreferredWidth(20);
        columnModel.getColumn(4).setPreferredWidth(20);
    }

    public void LlenarTablaConicidencias(javax.swing.event.DocumentEvent evt) {

        String campo_del_evento = (String) evt.getDocument().getProperty("campoDelEvento");
        List<Vehiculo> listaPersonas = new ArrayList<>();

        if (campo_del_evento.equals("descripcion")) {
            listaPersonas = VehiculoRN.getVehiculosLike(tf_descripcion.getText(), VehiculoJpaController.DESCRIPCION);
        }
        if (campo_del_evento.equals("nro_unidad")) {
            listaPersonas = VehiculoRN.getVehiculosLike(tf_nro_unidad.getText(), VehiculoJpaController.NRO_UNIDAD);
        }        
        if (campo_del_evento.equals("nro_chapa")) {
            listaPersonas = VehiculoRN.getVehiculosLike(tf_chapa.getText(), VehiculoJpaController.NRO_CHAPA);
        }
        if (campo_del_evento.equals("chofer")) {
            listaPersonas = VehiculoRN.getVehiculosLike(tf_chofer.getText(), VehiculoJpaController.CHOFER);
        }
        

        iniciarModeloTabla();

        for (Vehiculo c : listaPersonas) {

            String fila[] = new String[]{
                c.getId().toString(), c.getDescripcion(), c.getNro_unidad(), c.getChapa() , c.getNro_chasis()
            };
            modelo_de_tabla.addRow(fila);
        }
        tabla_conincidencias.setModel(modelo_de_tabla);
        iniciarModeloColumna();
    }

    public class KeyPressedClass implements KeyListener {

        @Override
        public void keyTyped(KeyEvent e) {
            accion(e);
        }

        @Override
        public void keyPressed(KeyEvent e) {
            accion(e);
        }

        @Override
        public void keyReleased(KeyEvent e) {
            accion(e);
        }

        public void accion(KeyEvent e) {
            if (e.getSource() == tf_descripcion || e.getSource() == tf_nro_unidad || e.getSource() == tf_chofer || e.getSource() == tf_chapa) {
                if (e.VK_DOWN == e.getKeyCode()) {
                    tabla_conincidencias.requestFocus();
                    tabla_conincidencias.changeSelection(0, 0, false, false);
                }
                if (e.getSource() == tf_descripcion) {

                }
            }
        }
    }
    
    public class KeyPressedEnterClass implements KeyListener {

        @Override
        public void keyTyped(KeyEvent e) {
            accion(e);
        }

        @Override
        public void keyPressed(KeyEvent e) {
            accion(e);
        }

        @Override
        public void keyReleased(KeyEvent e) {
            accion(e);
        }

        public void accion(KeyEvent e) {
            if (!tf_litros_a_cargar.getText().equals("")) {
                if (e.getSource() == tf_litros_a_cargar) {
                    if (e.VK_ENTER == e.getKeyCode()) {
                        int op = JOptionPane.showConfirmDialog(null, "Confirmar carga?");
                        if (op == 0) {
                            efectuarCarga();
                            limpiarCampos();
                            iniciarModeloTabla();
                            tabla_conincidencias.setModel(modelo_de_tabla);
                            habilitarCampos(true);
                            vehiculo_seleccionado = null;
                            tanque_selected = null;
                            tf_descripcion.requestFocus();
                        }
                    }

                }
            }
        }
   
    }

    private void llenarCampos(Vehiculo resul) {
        tf_descripcion.setText(resul.getDescripcion());
        tf_chapa.setText(resul.getChapa());
        tf_nro_unidad.setText(resul.getNro_unidad());
        tf_chofer.setText(resul.getChofer().toString());
        tf_marca.setText(resul.getModelo().getMarca().getDescripcion());
        tf_modelo.setText(resul.getModelo().getDescripcion());
    }

    private void habilitarCampos(boolean b) {
        tf_descripcion.setEditable(b);
        tf_chapa.setEditable(b);
        tf_nro_unidad.setEditable(b);
        tf_marca.setEditable(b);
        tf_modelo.setEditable(b);
        tf_chofer.setEditable(b);
    }
    
    class JTextFieldToUpperCase extends PlainDocument {

        public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException {
            super.insertString(offset, str.toUpperCase(), attr);
        }
    }
    
    private String getHora(){
        Calendar calendario = Calendar.getInstance();
        int hora, minutos, segundos;
        hora =calendario.get(Calendar.HOUR_OF_DAY);
        minutos = calendario.get(Calendar.MINUTE);
        //segundos = calendario.get(Calendar.SECOND);
        return hora + ":" + minutos;
    }
    
    private void efectuarCarga() {
        blanquearCampos();
        if (chaquearCamposRequeridos()) {
            if (verificarValor(tf_litros_a_cargar.getText())) {
                if (verificarCapacidadTanque()) {
                    entidades.SalidaCombustible salida = new entidades.SalidaCombustible();
                    salida.setVehiculo(vehiculo_seleccionado);
                    salida.setTipoCombustible(tanque_selected.getTipoCombustible());
                    salida.setTanque(tanque_selected);
                    salida.setResponsable(Principal.usuario);
                    salida.setLitros_cargados(Double.parseDouble(tf_litros_a_cargar.getText()));
                    salida.setHora(tf_hora.getText());
                    salida.setFecha(fecha.getDate());
                    salida.setDisponiblesEnTanque(tanque_selected.getDisponible_litros());
                    
                    CombustibleRN.create(salida);
                    
                    List<entidades.SalidaCombustible> ls_salida = tanque_selected.getLs_salidas();
                    ls_salida.add(salida);
                    
                    tanque_selected.setLs_salidas(ls_salida);
                    tanque_selected.setDisponible_litros(restarDecimales(tanque_selected.getDisponible_litros(), salida.getLitros_cargados()));
                    CombustibleRN.edit(tanque_selected);
                    JOptionPane.showMessageDialog(null, "Carga realizada!!!");
                    
                    reeStart();
                    
                    int op = JOptionPane.showConfirmDialog(null, "¿Imprimir Comprobante?");
                    if (op == 0) 
                        imprimirReporte();
                    
                } else {
                    tf_litros_a_cargar.setBackground(Color.PINK);
                    JOptionPane.showMessageDialog(null, "El valor ingresado excede la disponibilidad del tanque");
                }

            } else {
                JOptionPane.showMessageDialog(null, "El valor ingresado no es valido");
                tf_litros_a_cargar.setBackground(Color.PINK);
            }
        } else {
            JOptionPane.showMessageDialog(null, "Campos requeridos no pueden estar vacios!!!");
        }

    }
    
    private void reeStart() {
        limpiarCampos();
        iniciarModeloTabla();
        tabla_conincidencias.setModel(modelo_de_tabla);
        habilitarCampos(true);
        vehiculo_seleccionado = null;
        tf_combustible_disponible.setText("" + tanque_selected.getDisponible_litros());
        tf_descripcion.requestFocus();
    }
    
    private void blanquearCampos(){
        tf_litros_a_cargar.setBackground(Color.WHITE);
    }
    
    private boolean chaquearCamposRequeridos() {

        if (tf_litros_a_cargar.getText().startsWith(" ") || tf_litros_a_cargar.getText().isEmpty()) {
            tf_litros_a_cargar.setBackground(Color.PINK);
            return false;
        }

        return true;
    }
    
    private boolean verificarValor(String text) {       
        return (text.matches("[+]?\\d*(\\.\\d+)?") && text.equals("")==false);
    }
    
    private boolean verificarCapacidadTanque() {
        BigDecimal disponibles = new BigDecimal(tanque_selected.getDisponible_litros());
        BigDecimal litros_a_cargar = new BigDecimal(tf_litros_a_cargar.getText());        
        int resto = disponibles.compareTo(litros_a_cargar);                
        if(resto >= 0)
            return true;
        return false;        
    }
    
    private double restarDecimales(double disponible_litros, double litros_cargados) {
        BigDecimal disponibles = new BigDecimal(disponible_litros);
        BigDecimal litros_a_cargar = new BigDecimal(litros_cargados);
        litros_a_cargar = litros_a_cargar.negate();
        return disponibles.add(litros_a_cargar).doubleValue();
    }
    
    private void imprimirReporte() {
        Long id = CombustibleRN.getIdUltimoSalida();
        System.out.println("ID SALIDA: " + id);
        GenReportes g = new GenReportes();
        g.repSalidaCombustible(id);
    }

}
